package com.dg11185.nearshop.baidumap;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.android.nearshop.R;
import com.baidu.mapapi.map.BaiduMap.OnMapStatusChangeListener;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;

/**
 * 模拟百度地图放大缩小的自定义组合控件
 * 
 * @author：Daibo
 * @time：2014-6-23 上午11:17:40
 */
public class ZoomLayout extends LinearLayout {

    private static final int DEFAULT_ZOOM_LEVEL = 14;

    private static final int SMALLEST_ZOOM_LEVEL = 3;

    private static final int LARGEST_ZOOM_LEVEL = 19;

    private ImageView zoomIn, zoomOut;

    private int zoomLevel = DEFAULT_ZOOM_LEVEL;

    private ZoomClickListener listener = null;

    private MapView mMapView = null;

    private BaiduMap mBaiduMap = null;

    public ZoomLayout(Context context) {
        super(context);
        init(context);
    }

    public ZoomLayout(Context context, AttributeSet attrs) {

        super(context, attrs);
        init(context);
    }

    public void init(Context context) {
        RelativeLayout layout = (RelativeLayout) LayoutInflater.from(context)
                .inflate(R.layout.zoom_in_out_layout, null);
        addView(layout);
        layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        zoomIn = (ImageView) layout.findViewById(R.id.map_zoom_in);
        zoomOut = (ImageView) layout.findViewById(R.id.map_zoom_out);
        zoomIn.setOnClickListener(new ZoomInListener());
        zoomOut.setOnClickListener(new ZoomOutListener());

        findViewById(R.id.map_zoom_relocation).setOnClickListener(new RelocationListener());

    }

    /** 设置用于回调的Listener */
    public void setListener(ZoomClickListener listener) {
        this.listener = listener;
    }

    /** 传入MapView，监听他的状态变化 */
    public void setMapView(MapView mapView) {
        this.mMapView = mapView;
        this.mBaiduMap = mMapView.getMap();
        mBaiduMap.setOnMapStatusChangeListener(statusListener);
    }

    /** 放大 */
    private class ZoomInListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            zoomLevel = (int) mBaiduMap.getMapStatus().zoom;
            if (zoomLevel < LARGEST_ZOOM_LEVEL) {
                if (zoomLevel == SMALLEST_ZOOM_LEVEL) {
                    zoomOut.setImageResource(R.drawable.map_zoom_out);
                }
                zoomLevel++;
                if (zoomLevel == LARGEST_ZOOM_LEVEL) {
                    zoomIn.setImageResource(R.drawable.map_zoom_in_disable);
                }
                if (listener != null) {
                    listener.onZoomClicked(zoomLevel);
                }
            }
        }
    }

    /** 缩小 */
    private class ZoomOutListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            zoomLevel = (int) mBaiduMap.getMapStatus().zoom;
            if (zoomLevel > SMALLEST_ZOOM_LEVEL) {
                if (zoomLevel == LARGEST_ZOOM_LEVEL) {
                    zoomIn.setImageResource(R.drawable.map_zoom_in);
                }
                zoomLevel--;
                if (zoomLevel == SMALLEST_ZOOM_LEVEL) {
                    zoomOut.setImageResource(R.drawable.map_zoom_out_disable);
                }

                if (listener != null) {
                    listener.onZoomClicked(zoomLevel);
                }
            }
        }

    }

    /** 监听MapView状态变化，缩放等级改变时修改按钮状态 */
    private OnMapStatusChangeListener statusListener = new OnMapStatusChangeListener() {

        @Override
        public void onMapStatusChange(MapStatus arg0) {
        }

        @Override
        public void onMapStatusChangeFinish(MapStatus arg0) {
            int lastLevel = zoomLevel;
            zoomLevel = (int) mBaiduMap.getMapStatus().zoom;
            if (lastLevel == SMALLEST_ZOOM_LEVEL && zoomLevel > lastLevel) {
                zoomOut.setImageResource(R.drawable.map_zoom_out);
            }

            if (lastLevel == LARGEST_ZOOM_LEVEL && zoomLevel < lastLevel) {
                zoomIn.setImageResource(R.drawable.map_zoom_in);
            }
            if (lastLevel != SMALLEST_ZOOM_LEVEL && zoomLevel == SMALLEST_ZOOM_LEVEL) {
                zoomOut.setImageResource(R.drawable.map_zoom_out_disable);
            }
            if (lastLevel != LARGEST_ZOOM_LEVEL && zoomLevel == LARGEST_ZOOM_LEVEL) {
                zoomIn.setImageResource(R.drawable.map_zoom_in_disable);
            }

        }

        @Override
        public void onMapStatusChangeStart(MapStatus arg0) {
        }

    };

    private class RelocationListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onRelocationsClicked();
            }
        }

    }

    public interface ZoomClickListener {
        /** 放大回调 */
        public void onZoomClicked(int zoomLevel);

        /** 重定位回调 */
        public void onRelocationsClicked();
    }

}
